package com.ke.rule.base;

import com.ke.rule.base.ValidationException.Builder;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 服务方法的基础实现
 * 
 * @author guyu
 *
 * @param <R>
 */
public abstract class ServiceMethodBase<R> extends DecisionBasedServiceMethod<R> {
  private static final Logger logger = LoggerFactory.getLogger(ServiceMethodBase.class);
  
  protected ServiceMethodContext context;
  
  // 决策执行的步骤实现
  private DecisionExecutionStep step;
  
  public ServiceMethodBase(ApplicationServiceBase service, Object value) {
    this(service, value, null);
  }
  
  public ServiceMethodBase(ApplicationServiceBase service, Object value, Object oldValue) {
    super(service, value, oldValue);
    this.context = new ServiceMethodContext(this);
    this.step = new DecisionExecutionStep(this, logger);
  }

  @Override
  public ServiceMethodContext getContext() {
    return context;
  }

  @Override
  protected R doWork() {
    if (logger.isDebugEnabled()) {
      logger.debug("method input parameters ==> [value: {}, oldValue: {}]", getValue(), getOldValue());
    }
    return super.doWork();
  }

  @Override
  protected Builder proceedToStep() {
    if (ArrayUtils.isEmpty(getDecisionTableFiles())) {
      return null;
    }
    return step.shift();
  }
  
}
